// components.cpp
// Customizer
// Protea Customizer / Business Logic for managing components
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//////////////////////////////////////////////////////////////////////////////
//
// COMPONENT_DEFN Transactions
//
//////////////////////////////////////////////////////////////////////////////

//
//@ CHA ChgComponent
//@ COD ChgComponent
//
//@ GEN ChgComponent will modify a defined component for an application
//
TRANSACTION_CODE( ChgComponent ) 
{
	XCBuffer* TransBuf = NULL;
	XCBuffer* OrigBuf = NULL;
	bool ReviewOnly = false;

	//@ GRS Start the transaction
	START_TXN( ChgComponent );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_COMPONENT_DEFN, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		ReviewOnly = txnvars.buffer->IsBlank( 2, FLD_APPL_NBR, FLD_COMPONENT_NAME );
		START_DB_TXN();
			txnvars.Errors->Check();
			OrigBuf = TransBuf->Copy();
			ComponentVerify( TransBuf, ReviewOnly ? VERIFY_REVIEW : VERIFY_CHANGE, &txnvars, OrigBuf );
			txnvars.Errors->Check();

			if (!ReviewOnly) {
				OrigBuf->MoveField( TransBuf, FLD_COMPONENT_ID );

				//@ SUB ComponentChange change field record
				ComponentChange( OrigBuf, TransBuf, &txnvars );
			};

			LOG_TRANSACTION();

		END_DB_TXN();

		//@ Send back result buffer
		if (ReviewOnly)
			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
		else
			txnvars.buffer->MoveCorresponding( OrigBuf, txnvars.Errors  );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 2, &TransBuf, &OrigBuf );
	END_TXN();
	//@ GRE End the transaction
};


//
//@ CHA RevComponents
//@ COD RevComponents
//
//@ GEN RevComponents will return the defined components for an application
//
TRANSACTION_CODE( RevComponents ) {
	XCBuffer* TransBuf = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( RevComponents );
		XCSQLGen* query = txnvars.query;

		//@ Create transaction defn buffer
		TransBuf = App->CreateBuffer( FMT_COMPONENT_DEFN, true );

		//@ Move in screen values
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			XCOrder ord;

			query->MakeSELECT( TransBuf );
			query->AddWHERE( 1, TransBuf->GetField( FLD_APPL_NBR ) );
			query->AddORDERBY( FLD_COMPONENT_ID, &ord );
			stmt = txnvars.dbtxn->ExecuteSQL( query );

			while (!stmt->Eof()) {
				App->TransResponse( txnvars.result, TransBuf, TRANS_DETAIL_TYPE );
				stmt->MoveNext();
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 2, &TransBuf, &stmt );
	END_TXN();
	//@ GRE End the transaction
};
